var rootPath = getRootPath();

$(function() {
	$("#password,#password2").focus(function() {
		hideMsg();
	});
	
	$("#password,#password2").bind('input propertychange',function() {
		if ($("#password").val() == '' || $("#password2").val() == ''
			|| $.trim($("#password").val()).length < 6 || $.trim($("#password").val()).length > 16
			|| $("#password").val() != $("#password2").val()) {
			$("#save").css({"background" : "#bbbbbb","cursor" : "default"});
			$("#save").attr("disabled", true);
		} else {
			$("#save").css({"background" : "rgb(197,24,10)","cursor" : "pointer"});
			$("#save").attr("disabled", false);
		}
	});

	$("#password").blur(function() {
		const password = $(this).val();
		
		if($.trim($(this).val()) == '') {
			showErrMsg("请输入新密码");
			return;
		} else if($.trim($(this).val()).length < 8 || $.trim($(this).val()).length > 16) {
			showErrMsg("密码长度无效,应为8-16位");
			return;
		}
		var pattern = /\s+/;
		if(pattern.test($(this).val())) {
			showErrMsg("密码包含空格,请重新输入");
			return;
		}
		if($("#password2").val() != "" && $("#password2").val() != $(this).val()) {
			showErrMsg("密码不一致,请重新输入");
			return;
		}
		const result = checkPasswordStrength(password);
		updateStrengthDisplay(result.level, result.types);
        if (result.level == "weak" || result.level == "") {
			showErrMsg("密码强度态度，请使用大小写英文、数字、符号等三种以上密码");
			return;
		}
	});
	
	$("#password2").blur(function() {
		if($(this).val() == '') {
			showErrMsg("请再次输入新密码");
			return;
		} else if($.trim($("#password").val()).length < 8 || $.trim($("#password").val()).length > 16) {
			showErrMsg("密码长度无效,应为8-16位");
			return;
		}
		var pattern = /\s+/;
		if(pattern.test($(this).val())) {
			showErrMsg("密码包含空格,请重新输入");
			return;
		}
		if($("#password").val() != $(this).val()) {
			showErrMsg("密码不一致,请重新输入");
			return;
		}
	});
	
	$("#save").click(function() {
		const password = $.trim($("#password").val());
		if ($.trim($("#password").val()) == "" || $.trim($("#password2").val()) == "") {
			showErrMsg("请输入新密码");
			return;
		}
		
		var pattern = /\s+/;
		if(pattern.test($("#password").val()) || pattern.test($("#password2").val())) {
			showErrMsg("密码包含空格,请重新输入");
			return;
		}

		if ($("#password").val() != $("#password2").val()) {
			showErrMsg("密码不一致,请重新输入:)");
			return;
		}
		
		const result = checkPasswordStrength(password);
		updateStrengthDisplay(result.level, result.types);
        if (result.level == "weak" || result.level == "") {
			showErrMsg("密码强度太低，请使用大小写英文、数字、符号等三种以上密码");
			return;
		}

		showLoading();
		$.ajax({
			url : rootPath + '/loginFront/updateLogin',
			type : 'POST',
			data : JSON.stringify($("#setNewPwdForm").serializeJson()),
			contentType : 'application/json;charset=UTF-8',
			success : function(data) {
				hideLoading();
				if (data.returnCode == '0000') {
					layer.msg("更新成功", {icon : 1,time : 1000}, function() {
						parent.$("#content").prop("src","securitySetting.html");
					});
				} else {
					hideLoading();
					layer.alert(data.returnMessage);
				}
			},
			error : function(jqXHR, textStatus, errorThrown) {
				layer.alert("error");
			}
		});
	});
	
	$('#password').on('input', function() {
        const password = $(this).val();
        const result = checkPasswordStrength(password);
        updateStrengthDisplay(result.level, result.types);
      });
	

});

// 密码强度检测核心逻辑[1,6](@ref)
function checkPasswordStrength(password) {
	const hasLower = /[a-z]/.test(password);  // 小写字母
	const hasUpper = /[A-Z]/.test(password);  // 大写字母
	const hasNumber = /[0-9]/.test(password);  // 数字
	const hasSymbol = /[^a-zA-Z0-9]/.test(password);  // 特殊符号

	// 统计字符类型数量
	const typeCount = [hasLower, hasUpper, hasNumber, hasSymbol].filter(Boolean).length;

	// 根据规则判定强度[3](@ref)
	let level = 'weak';
	if (password.length === 0) {
		level = '';
	} else if (typeCount === 4 && password.length >= 8) {
		level = 'strong';
	} else if (typeCount >= 3 && password.length >= 8) {
		level = 'medium';
	} else {
		level = 'weak';
	}

	return {
		level,
		types: { hasLower, hasUpper, hasNumber, hasSymbol }
	};
}
// 更新UI显示[3,6](@ref)
function updateStrengthDisplay(level, types) {
	const $bar = $('#strengthBar');
	const $text = $('#strengthText');

	switch (level) {
		case 'strong':
			$bar.css({
				'width': '100%',
				'background': '#52c41a'
			});
			$text.text('强：包含四种字符类型且长度足够').addClass('strong');
			break;
		case 'medium':
			$bar.css({
				'width': '66%',
				'background': '#faad14'
			});
			$text.text('中：包含三种字符类型且长度≥8').addClass('medium');
			break;
		case 'weak':
			$bar.css({
				'width': '33%',
				'background': '#ff4d4f'
			});
			$text.text('弱：请补点大写字母或符号（如#）').addClass('weak');
			break;
		default:
			$bar.css('width', '0%');
			$text.text('').removeClass();
	}
}



function showErrMsg(msg){
	$("#pwd_msg").text(msg);
	$(".msg").show();
}

function hideMsg() {
	$("#pwd_msg").text("");
	$(".msg").hide();
}